package com.club.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.common.exception.BusinessException;
import com.club.entity.ClubMember;
import com.club.mapper.ClubMemberMapper;
import com.club.service.ClubMemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 俱乐部成员服务实现类
 */
@Service
public class ClubMemberServiceImpl extends ServiceImpl<ClubMemberMapper, ClubMember> implements ClubMemberService {

    @Override
    @Transactional
    public boolean addMember(ClubMember clubMember) {
        // 检查是否已经是成员
        ClubMember existingMember = this.getBaseMapper().selectByClubAndUser(clubMember.getClubId(), clubMember.getUserId());
        if (existingMember != null && existingMember.getStatus() == 0) {
            throw new BusinessException("用户已经是俱乐部成员");
        }
        
        // 设置默认值
        clubMember.setStatus(0);
        clubMember.setJoinTime(LocalDateTime.now());
        
        // 如果是新成员，直接保存
        if (existingMember == null) {
            return this.save(clubMember);
        } else {
            // 如果是曾经退出的成员，更新状态
            existingMember.setStatus(0);
            existingMember.setRole(clubMember.getRole());
            existingMember.setJoinTime(LocalDateTime.now());
            return this.updateById(existingMember);
        }
    }

    @Override
    @Transactional
    public boolean removeMember(Long clubId, Long userId) {
        ClubMember member = this.getBaseMapper().selectByClubAndUser(clubId, userId);
        if (member == null || member.getStatus() != 0) {
            throw new BusinessException("用户不是俱乐部成员");
        }
        
        // 软删除，更新状态
        member.setStatus(1);
        return this.updateById(member);
    }

    @Override
    @Transactional
    public boolean updateMemberRole(Long clubId, Long userId, Integer role) {
        ClubMember member = this.getBaseMapper().selectByClubAndUser(clubId, userId);
        if (member == null || member.getStatus() != 0) {
            throw new BusinessException("用户不是俱乐部成员");
        }
        
        member.setRole(role);
        return this.updateById(member);
    }

    @Override
    public List<ClubMember> getMembersByClubId(Long clubId) {
        return this.getBaseMapper().selectByClubId(clubId);
    }

    @Override
    public ClubMember getMemberByClubAndUser(Long clubId, Long userId) {
        return this.getBaseMapper().selectByClubAndUser(clubId, userId);
    }

    @Override
    public boolean isMember(Long clubId, Long userId) {
        ClubMember member = this.getBaseMapper().selectByClubAndUser(clubId, userId);
        return member != null && member.getStatus() == 0;
    }

    @Override
    public boolean isAdmin(Long clubId, Long userId) {
        ClubMember member = this.getBaseMapper().selectByClubAndUser(clubId, userId);
        return member != null && member.getStatus() == 0 && (member.getRole() == 1 || member.getRole() == 2);
    }
}